package com.huifaapp.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.Homefragment.MessagetwoActivity;
import com.huifaapp.mall.bean.MeaageBean;

import java.util.List;

/**
 * Created by mac on 2018/1/30.
 */

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MyViewHolder> {

    private Context context;
    private List<MeaageBean.DataBean>list;

    public MessageAdapter(Context context, List<MeaageBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.message_item,parent,false);
        return new MyViewHolder(view) ;

    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.mess_tv.setText(list.get(position).getName());
        Glide.with(context).load(list.get(position).getIcon()).into(holder.mess_im);
        if (list.get(position).getCount()==0){
            holder.mess_count.setVisibility(View.GONE);
        }else {
            holder.mess_count.setVisibility(View.VISIBLE);
        }
        holder.mess_count.setText(list.get(position).getCount()+"");


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, MessagetwoActivity.class);
                intent.putExtra("parentId",list.get(position).getId());
                context.startActivity(intent);
            }
        });


    }



    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }



    public class MyViewHolder extends RecyclerView.ViewHolder {


        ImageView mess_im;
        TextView mess_tv;
        TextView mess_count;


        public MyViewHolder(View itemView) {
            super(itemView);
            mess_im = (ImageView) itemView.findViewById(R.id.message_item_im);
            mess_tv = (TextView) itemView.findViewById(R.id.message_item_tv);
            mess_count = (TextView) itemView.findViewById(R.id.message_item_count);
        }
    }


    public interface OnItemClickListener {
        void onitem(int position);


    }
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

}

